/*
 *  Copyright (C) 2011 Hatsites <Hatsites_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.shr.dtb.language;

import java.sql.ResultSet;
import org.myorg.shr.dtb.adaptator.AdaptatorJdbc;
import org.myorg.shr.dtb.language.dictionary.DictionaryMysql;
import org.myorg.shr.dtb.structure.dtb.DtbAbs;
import org.myorg.shr.dtb.structure.db.table.TblAbs;

/**
 *
 * @author Hatsites
 *
 * email: Hatsites_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */

public class LanguageShrMysql extends LanguageShrAbs{
    private LanguageShrMysql() throws Exception{
        this._jdbcAdaptator_ = AdaptatorJdbc.s_getInstance();
    }
    public static synchronized LanguageShrMysql s_getInstance() throws Exception {
        if (_INSTANCE_ == null) {
                _INSTANCE_ = new LanguageShrMysql();
        }
        return _INSTANCE_;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    @Override
    public void doCreateDb(String user, String password, DtbAbs db)
            throws Exception{
        this._jdbcAdaptator_.connect(db.getUrl(), user, password);
        this._jdbcAdaptator_.executeUpdate(
            DictionaryMysql.createDB(db.getName()));
        this._jdbcAdaptator_.executeUpdate(
            DictionaryMysql.useDB(db.getName()));

        int nbTables =  db.countTables();
        for(int i=0; i < nbTables; ++i){
            TblAbs tab = db.getTable(i);
            String strCmd = DictionaryMysql.createTable(tab.getName());
            int nbFields_1 =  tab.countFields() - 1;

            //Last field has a different treatment, thats why - 1
            for (int j=0; j<nbFields_1; ++j){
                strCmd += DictionaryMysql.createField(tab, j);//.getFieldName(j), DictionaryMysql.getFieldCommand(tab.getFieldType(j)));
            }

            strCmd += DictionaryMysql.createLastField(tab.getFieldName(nbFields_1), DictionaryMysql.typVarchar(20));
            this._jdbcAdaptator_.executeUpdate(strCmd);
        }
        this._jdbcAdaptator_.disconnect();
    }

    @Override
    public void doDropDb(String user, String password, DtbAbs db) throws Exception {
        this._jdbcAdaptator_.connect(db.getUrl(), user, password);
        this._jdbcAdaptator_.executeUpdate(
                DictionaryMysql.dropDB(db.getName()));
        this._jdbcAdaptator_.disconnect();
    }
   
    private static LanguageShrMysql _INSTANCE_;
    private AdaptatorJdbc _jdbcAdaptator_;

    @Override
    public void doAddRecording(String user, String password, DtbAbs dtb, TblAbs tbl, String strKeyId) throws Exception {
        this._jdbcAdaptator_.connect(dtb.getUrl(), user, password);
        this._jdbcAdaptator_.executeUpdate(DictionaryMysql.useDB(dtb.getName()));
        this._jdbcAdaptator_.executeQuerry(DictionaryMysql.countElements(tbl.getName()));
        ResultSet result = this._jdbcAdaptator_.getResult();
        Integer intLastId = 0;
        while(result.next()) {
            intLastId = result.getInt(1);
        }
        intLastId++;
        String strId = intLastId.toString();
        tbl.setValue(strKeyId, strId);

        String strCommand = "";
        strCommand += DictionaryMysql.insertIntoTable(tbl.getName());
        int last = tbl.countFields() - 1;
        for (int i = 0; i < last ; ++i){
            strCommand += DictionaryMysql.addField(tbl.getValue(i));
        }
        strCommand += DictionaryMysql.addLastField(tbl.getValue(last));

        this._jdbcAdaptator_.executeUpdate(strCommand);
        this._jdbcAdaptator_.disconnect();

    }
}
